package com.example.test.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.test.bean.BraendType;
import com.example.test.bean.DTO.input.ResetPwdDTO;
import com.example.test.bean.DTO.input.UsersCreateInputDTO;
import com.example.test.bean.DTO.input.UsersUpdateInputDTO;
import com.example.test.common.model.PageModel;
import com.example.test.common.model.R;
import com.example.test.common.token.LoginToken;
import com.example.test.service.UsersService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>
 * 管理员信息表 前端控制器
 * </p>
 *
 * @author dz
 * @since 2022-04-10
 */

@CrossOrigin
@RestController
@RequestMapping("/users")
public class UsersController {
    @Resource
    private UsersService usersService;

    @RequestMapping("/login")
    public R<JSONObject> login(@RequestBody JSONObject jsonParam) {
//
        System.out.println(jsonParam.toJSONString());
        String username = jsonParam.getString("username");
        String password = jsonParam.getString("password");
        return usersService.login(username,password);
    }
    @LoginToken
    @ApiOperation(value = "商品信息表根据keyword分页查询")
    @GetMapping("/getByKeyword")
    public R usersList( String keyword, PageModel pageModel) {
        return usersService.getTypeByKeyword(keyword,pageModel);
    }
    @LoginToken
    @ApiOperation(value = "根据id分页查询")
    @PostMapping("/getUser")
    public R listById(@RequestBody JSONObject jsonParam) {
        Long userId = Long.valueOf(jsonParam.getString("userId"));
        return usersService.getUserById(userId);
    }

    @ApiOperation(value = "添加")
    @PostMapping(value = "/insert")
    public R insert(  UsersCreateInputDTO usersCreateInputDTO) {
        return usersService.addUser(usersCreateInputDTO);
    }

    @LoginToken
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public R update(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        UsersUpdateInputDTO usersUpdateInputDTO = new UsersUpdateInputDTO();
        usersUpdateInputDTO.setUserId(jsonParam.getLong("userId"));
        usersUpdateInputDTO.setUserName(jsonParam.getString("username"));
        usersUpdateInputDTO.setPassword(jsonParam.getString("password"));
        return usersService.updateUser(usersUpdateInputDTO);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/delete")
    public R<Boolean> delete(@Validated @ApiParam  Long userId) {
        return usersService.deleteUser(userId);
    }



    @LoginToken
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/resetPwd")
    public R resetPassowrd(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        ResetPwdDTO resetPwdDTO = new ResetPwdDTO();
        resetPwdDTO.setUserId(jsonParam.getLong("userId"));
        resetPwdDTO.setCurrentPass(jsonParam.getString("currentPass"));
        resetPwdDTO.setPassword(jsonParam.getString("password"));
        return usersService.resetPwd(resetPwdDTO);
    }


    @PostMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }

}

